package com.cn.kehong.service;

import com.cn.kehong.api.IExceptionManagementService;
import com.cn.kehong.conf.Config;
import com.cn.kehong.dao.ExceptionManagementMapper;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.GenerateUUID;
import com.cn.kehong.utils.QueryForm;
import com.cn.kehong.utils.TemplateExcelUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ExceptionManagementServiceImpl implements IExceptionManagementService {

    @Autowired
    private ExceptionManagementMapper exceptionManagementMapper;

    @Autowired
    private GenerateUUID generateUUID;
    @Autowired
    private Config config;
    @Override
    public List<ExceptionManagement> getExceptionManagementList(QueryForm queryForm) throws Exception {
        //待评审
        if (queryForm.getType().equals("0")){
            ArrayList<String> list = new ArrayList<>();
            list.add("0");
            list.add("1");
            queryForm.setTypeList(list);
            int count = exceptionManagementMapper.getExceptionManagementCount(queryForm);
            if (count == 0) {
                return Lists.newArrayList();
            }
            List<ExceptionManagement> dataSubmissions = exceptionManagementMapper.getExceptionManagementList(queryForm);
            dataSubmissions.get(0).setCount(count);
            return dataSubmissions;
         //已处置
        }else if (queryForm.getType().equals("1")){
            ArrayList<String> list = new ArrayList<>();
            list.add("2");
            queryForm.setTypeList(list);
            int count = exceptionManagementMapper.getExceptionManagementCount(queryForm);
            if (count == 0) {
                return Lists.newArrayList();
            }
            List<ExceptionManagement> dataSubmissions = exceptionManagementMapper.getExceptionManagementList(queryForm);
            dataSubmissions.get(0).setCount(count);
            return dataSubmissions;
        //已取消
        }else if (queryForm.getType().equals("2")){
            ArrayList<String> list = new ArrayList<>();
            list.add("3");
            queryForm.setTypeList(list);
            int count = exceptionManagementMapper.getExceptionManagementCount(queryForm);
            if (count == 0) {
                return Lists.newArrayList();
            }
            List<ExceptionManagement> dataSubmissions = exceptionManagementMapper.getExceptionManagementList(queryForm);
            dataSubmissions.get(0).setCount(count);
            return dataSubmissions;
    }
        return null;
    }

    @Override
    public String exceptionManagementCancle(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        ExceptionManagement exceptionManagement = new ExceptionManagement();
        exceptionManagement.setUpdateTime(new Date());
        exceptionManagement.setYcStatus("3");
        exceptionManagement.setDisposalStatus("已取消");
        ExceptionManagementExample example = new ExceptionManagementExample();
        example.createCriteria().andIdEqualTo(id);
        int count = exceptionManagementMapper.updateByExampleSelective(exceptionManagement,example);
        if (count < 1) {
            return  "取消失败！";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String temporarily(ExceptionManagement exceptionManagement) throws Exception {
        if (null == exceptionManagement){
            return "请输入异常信息";
        }
        if (StringUtils.isNotBlank(exceptionManagement.getId())){
            ExceptionManagementExample example = new ExceptionManagementExample();
            example.createCriteria().andIdEqualTo(exceptionManagement.getId());
            int count = exceptionManagementMapper.updateByExampleSelective(exceptionManagement,example);
            if (count < 1) {
                return  "暂存失败！";
            }
        }else {
            String id = CommonUtil.generateRandomNum("exceptionManagement_");
            exceptionManagement.setId(id);
            exceptionManagement.setYcStatus("0");
            exceptionManagement.setDisposalStatus("待上报");
            int count = exceptionManagementMapper.insertSelective(exceptionManagement);
            if (count < 1) {
                throw  new Exception("暂存失败！");
            }
            return CommonUtil.outStr(count);
        }
        return null;
    }

    @Override
    public String addExceptionManagement(ExceptionManagement exceptionManagement) throws Exception {
        if (null == exceptionManagement){
            return "请输入异常信息";
        }
        String result = this.exceptionManagementChange(exceptionManagement);
        if (StringUtils.isNotEmpty(result)){
            return result;
        }
        String id = CommonUtil.generateRandomNum("exceptionManagement_");
        exceptionManagement.setId(id);
        exceptionManagement.setYcStatus("1");
        exceptionManagement.setCreateTime(new Date());
        exceptionManagement.setUpdateTime(new Date());
        //号段 年月日日两位+三位流水号
        String yearMoth = generateUUID.getYearMoth();
        String key = "phy"+yearMoth;
        int num = generateUUID.getNumberFlag(key);
        LocalDateTime now = LocalDateTime.now();
        String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
        String year1 = year.substring(year.length() - 2);
        String month = now.format(DateTimeFormatter.ofPattern("MM"));
        String day = now.format(DateTimeFormatter.ofPattern("dd"));
        String sampleCode =year1+month+ day+generateUUID.getThreeNumStr(num);
        exceptionManagement.setNumber(sampleCode);
        exceptionManagement.setUpdateTime(new Date());
        int count = exceptionManagementMapper.insertSelective(exceptionManagement);
        if (count < 1) {
            throw  new Exception("新增失败！");
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateExceptionManagement(ExceptionManagement exceptionManagement) throws Exception {
        String id = exceptionManagement.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }
        String result = this.exceptionManagementChange(exceptionManagement);
        if (!"".equals(result)) {
            return result;
        }
        ExceptionManagementExample example = new ExceptionManagementExample();
        example.createCriteria().andIdEqualTo(id);
        exceptionManagement.setCreateTime(new Date());
        exceptionManagement.setUpdateTime(new Date());
        //号段 年月日日两位+三位流水号
        String yearMoth = generateUUID.getYearMoth();
        String key = "phy"+yearMoth;
        int num = generateUUID.getNumberFlag(key);
        LocalDateTime now = LocalDateTime.now();
        String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
        String year1 = year.substring(year.length() - 2);
        String month = now.format(DateTimeFormatter.ofPattern("MM"));
        String day = now.format(DateTimeFormatter.ofPattern("dd"));
        String sampleCode =year1+month+ day+generateUUID.getThreeNumStr(num);
        exceptionManagement.setNumber(sampleCode);
        exceptionManagement.setUpdateTime(new Date());
        int count = exceptionManagementMapper.updateByExampleSelective(exceptionManagement,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public void exportExceptionManagement(QueryForm queryForm, HttpServletResponse response) throws Exception {
        Map<String, Object> result = Maps.newHashMap();
        ArrayList<String> list = new ArrayList<>();
        list.add("2");
        queryForm.setTypeList(list);
        List<ExceptionManagement> dataSubmissions = exceptionManagementMapper.getExceptionManagementList(queryForm);
        result.put("list", dataSubmissions);
        String fileName = CommonUtil.generateRandomNum("ycgl-") + ".xlsx";
        String filePath = config.filePath + fileName;
        TemplateExcelUtils.downLoadExcel(filePath, "异常管理.xlsx", result, "ycgl.xlsx", response);

    }

    private String exceptionManagementChange(ExceptionManagement exceptionManagement) {
        String result = "";
        if (StringUtils.isBlank(exceptionManagement.getFactoryName())){
            result = "请输入厂房车间";
            return result;
        }
        if (StringUtils.isBlank(exceptionManagement.getSectionProcess())){
            result = "请输入工段工序";
            return result;
        }
        if (StringUtils.isBlank(exceptionManagement.getDisposalStatus())){
            result = "请输入处置状态";
            return result;
        }
        if (StringUtils.isBlank(exceptionManagement.getAbnormalLevel())){
            result = "请输入异常等级";
            return result;
        }
        if (StringUtils.isBlank(exceptionManagement.getAbnormalType())){
            result = "请输入异常类型";
            return result;
        }
        return result;
    }
}
